package com.group.mall.service.Impl;

import com.group.mall.domain.POJO.Goods;
import com.group.mall.domain.POJO.GoodsPre;
import com.group.mall.domain.POJO.GoodsPro;
import com.group.mall.domain.VO.GoodsVO;
import com.group.mall.domain.VO.ShowGoodsVO;
import com.group.mall.mapper.GoodsMapper;
import com.group.mall.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper itemMapper;

    //获取热门商品
    @Override
    public List<Goods> queryHotGoods() {
        //查询销量排名前10的商品
        List<Goods> goods = itemMapper.selectHotGoods();
        log.info("queryHotGoods: " + goods);
        return goods;
    }

    @Override
    public List<GoodsVO> queryItemsByName(String searchInfo, Integer chooseType) {
        //模糊查询商品信息
        List<Goods> goodsList = itemMapper.selectGoodsByName(searchInfo, chooseType);
        //查询商品推销信息
        List<GoodsVO> goodsVOList = new ArrayList<>();
        goodsList.forEach(goods -> {
            GoodsPre goodsPre = itemMapper.selectGoodsPreById(goods.getGoodsId());
            log.info("goodsPre:" + goodsPre);
            //封装商品列表vo
            GoodsVO goodsResult = new GoodsVO();

            goodsResult.setBrand(goods.getBrand());
            goodsResult.setName(goods.getName());
            goodsResult.setPreInfo(goodsPre.getPreInfo());
            goodsResult.setPrice(goods.getPrice());
            goodsResult.setDiscountInfo(goodsPre.getDiscountInfo());
            goodsResult.setImage(goods.getImage());

            goodsVOList.add(goodsResult);
        });

        log.info("queryItemsByName: " + goodsVOList);
        return goodsVOList;
    }

    @Override
    public Goods queryItemById(Integer id) {
        return itemMapper.selectGoodsById(id);
    }

    @Override
    public List<GoodsVO> queryItems(String searchInfo) {
        //模糊查询商品信息
        List<Goods> goodsList = itemMapper.selectGoods(searchInfo);
        //查询商品推销信息
        List<GoodsVO> goodsVOList = new ArrayList<>();

        goodsList.forEach(goods -> {
            GoodsPre goodsPre = itemMapper.selectGoodsPreById(goods.getGoodsId());

            log.info("goodsPre:" + goodsPre);
            //封装商品列表vo
            GoodsVO goodsResult = new GoodsVO();

            goodsResult.setBrand(goods.getBrand());
            goodsResult.setName(goods.getName());
            goodsResult.setPreInfo(goodsPre.getPreInfo());
            goodsResult.setPrice(goods.getPrice());
            goodsResult.setDiscountInfo(goodsPre.getDiscountInfo());
            goodsResult.setImage(goods.getImage());

            goodsVOList.add(goodsResult);
        });

        log.info("queryItemsByName: " + goodsVOList);
        return goodsVOList;
    }

    @Override
    public List<ShowGoodsVO> showItemById(Integer id) {
        List<ShowGoodsVO> ShowGoodsVOList = new ArrayList<>();
        ShowGoodsVO showGoodsResult = new ShowGoodsVO();

        GoodsPro goodsPro = itemMapper.selectGoodsProById(id);
        GoodsPre goodsPre = itemMapper.selectGoodsPreById(id);
        Goods goods = itemMapper.selectGoodsById(id);

        /*1*/
        showGoodsResult.setImg1(goodsPre.getImg1());
        showGoodsResult.setImg2(goodsPre.getImg2());
        showGoodsResult.setImg3(goodsPre.getImg3());
        showGoodsResult.setImg4(goodsPre.getImg4());
        showGoodsResult.setImg5(goodsPre.getImg5());
        showGoodsResult.setDiscountInfo(goodsPre.getDiscountInfo());
        showGoodsResult.setPreInfo(goodsPre.getPreInfo());

        /*2*/
        showGoodsResult.setVersion(goodsPro.getVersion());
        showGoodsResult.setColor(goodsPro.getColor());

        /*3*/
        showGoodsResult.setId(goods.getGoodsId());
        showGoodsResult.setName(goods.getName());
        showGoodsResult.setBrand(goods.getBrand());
        showGoodsResult.setCategory(goods.getCategory());
        showGoodsResult.setImage(goods.getImage());
        showGoodsResult.setPrice(goods.getPrice());
        showGoodsResult.setDiscountPrice(goods.getDiscountPrice());
        showGoodsResult.setStock(goods.getStock());
        showGoodsResult.setSold(goods.getSold());
        showGoodsResult.setScore(goods.getScore());

        ShowGoodsVOList.add(showGoodsResult);

        return ShowGoodsVOList;
    }

    @Override
    public List<GoodsVO> queryAllGoods() {
        List<Goods> goodsList = itemMapper.selectAllGoods();

        List<GoodsVO> goodsVOList = new ArrayList<>();

        goodsList.forEach(goods -> {
            GoodsPre goodsPre = itemMapper.selectGoodsPreById(goods.getGoodsId());

            log.info("goodsPre:" + goodsPre);
            //封装商品列表vo
            GoodsVO goodsResult = new GoodsVO();

            goodsResult.setBrand(goods.getBrand());
            goodsResult.setName(goods.getName());
            goodsResult.setPreInfo(goodsPre.getPreInfo());
            goodsResult.setPrice(goods.getPrice());
            goodsResult.setDiscountInfo(goodsPre.getDiscountInfo());
            goodsResult.setImage(goods.getImage());

            goodsVOList.add(goodsResult);
        });

        log.info("queryItemsByName: " + goodsVOList);
        return goodsVOList;
    }
}
